<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentAddAlbumSong.class.php, $Author:  $ */



/** Component for uploading songs into album
 *   
 * @version $Id: PhpFox_ComponentAddAlbumSong.class.php 332 2008-02-15 17:43:25Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentAddAlbumSong extends BaseComponent
{
    function process()
    {
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        $aOut = array();
        App::loadClass('Validator');
		$oAccountService = &App::getModuleService('Account', 'Account');
        $oSecurityService->checkPerm('music', array('full'));
        $oSecurityService->checkAccess('music');
        $iAlbumId = $oReq->getInt('id');
        if (!$iAlbumId)
        {
            App::gotoUrl('public.account.submit.music');
        }

        $aFields = array
        (
            'val[song_title]'    => array('title' => App::format('song_title'),    'def' => 'required'),
            'val[musician_name]' => array('title' => App::format('musician_name'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields);
        if ($oReq->getAction() == 'upload')
        {
            $aValues = $oReq->getArray('val');
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            $oAlbumItem    = &$this->_oModule->getItem('Album');
            $oAlbumItem->load($iAlbumId);
	        $oCurrentUser = $oSecurityService->getCurrentUser();
            if($oAlbumItem->aData['user_id']!=$oCurrentUser->aData['id'])
	        {
	        	App::gotoUrl('public.music');
	        }
            if ( substr($_FILES['song']['name'], -3) != 'mp3' )
            {
            	$this->addErrors(App::format('Mod_Music.error.wrong_format'));
            }
            if (!count($this->getErrors()))
            {
                $aSongData = array
                (
                    'song_title'    => htmlspecialchars($aValues['song_title']),
                    'musician_name' => htmlspecialchars($aValues['musician_name']),
                    'genre_id'      => $aValues['genre_id'],
                    'album_id'      => $iAlbumId,
                );
                
                $iUsedSpace = $oAccountService->getUsedSpace($oSecurityService->getCurrentUserLogin());
                $iAllowedSpace = $oAccountService->getAllowedSpace($oSecurityService->getCurrentUser());
                $iMaxUploadSize = App::getParam('Mod_Music.max_song_size');
                if (($iAllowedSpace != 0) && (($iAllowedSpace - $iUsedSpace) < $iMaxUploadSize))
                {
                    $iMaxUploadSize = $iAllowedSpace - $iUsedSpace;
                }                
                
                if (!$oMusicService->uploadSong('song', $aSongData, $oCurrentUser->aData['id'], $iMaxUploadSize))
                {
                    $this->addErrors($oMusicService->getErrors());
                }
            }
            if (count($this->getErrors()))
            {
            	App::gotoUrl('', array('id' => $iAlbumId), $this->getErrors(), true);
            } 
            else 
            {
                App::gotoUrl('', array('id' => $iAlbumId), App::format('Mod_Music.info.album_song_uploaded'), false);
            }
            $oTpl->assign(array(
                'aValue' => $aValues,
            ));
        }
        else
        {
            $aCond = array
            (
                'album_id' => '= '.$iAlbumId,
            );
            list($aAlbums) = $oMusicService->getAlbumsList($aCond);
            if (isset($aAlbums[0]))
            {
                $oTpl->assign(array(
                    'aValue' => $aAlbums[0],
                ));
            }
        }

        $aCond = array
        (
            'album_id' => '= '.$iAlbumId,
        );
        list($aSongs) = $oMusicService->getSongsList($aCond);

        $oTpl->assignSrc(array(
            'sValidatorJs'   => $oValidator->makeJS(),
            'sValidatorMess' => $oValidator->makeJsMess(),
        ));
        $oTpl->assign(array(
            'aErrors'      => $this->getErrors(),
            'aSongs'       => $aSongs,
            'aGenres'      => $oMusicService->getGenres(),
            'iMaxSongSize' => App::getParam('Mod_Music.max_song_size'),
        ));
        $aOut['main'] = $oTpl->fetch('UploadAlbumSong.html');
        $aOut['songs'] = $oTpl->fetch('UploadedSongs.html');
        return $aOut;
    }
}
?>